﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using Shsc.Net.FrameWork.Common.Log;
using Seasar.Framework.Container;
using Seasar.Framework.Container.Factory;

namespace Shsc.Net.FrameWork.Web.Control
{
    /// <summary>
    /// Seasar ControllerFactory
    /// </summary>
    public class SeasarControllerFactory : IControllerFactory
    {
        /// <summary>
        /// Looger
        /// </summary>
        private static Log4NetLogger logger = Log4NetLogger.CreateLogger(typeof(SeasarControllerFactory));

        /// <summary>
        /// ControlFactory
        /// </summary>
        private static DefaultControllerFactory defalutf = null;



        #region IControllerFactory 成员

        /// <summary>
        /// Creates the controller.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            IS2Container ctx = SingletonS2ContainerFactory.Container;
            if (ctx.HasComponentDef(controllerName))
            {
                object controllerf = ctx.GetComponent(controllerName);
                return (IController)controllerf;
            }
            else
            {
                if (defalutf == null)
                {
                    defalutf = new DefaultControllerFactory();
                }
                return defalutf.CreateController(requestContext, controllerName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="controller"></param>
        public void ReleaseController(IController controller)
        {
            IS2Container ctx = SingletonS2ContainerFactory.Container;
            if (!ctx.HasComponentDef(controller.GetType().Name.Replace("Controller", "")))
            {
                if (defalutf == null)
                {
                    defalutf = new DefaultControllerFactory();
                }
                defalutf.ReleaseController(controller);
            }
        }

        #endregion
    }
}
